#ifndef HTTPCONNECTION_H_
#define HTTPCONNECTION_H_

#include <iostream>
#include <sstream>
#include "ByteString.h"
#include "HighLevelConnection.h"
using namespace std;

class HTTPConnection: public HighLevelConnection {
public:
	HTTPConnection(int sockfd);
	virtual ~HTTPConnection();
	virtual int
	sendResponse(unsigned short flags, const char* buffer = NULL, unsigned int bufferSize = 0, unsigned int length = 0);
	virtual bool acceptsType(string type);
	virtual bool passesAuth(string userPass);

protected:
	virtual void listener();

private:
	bool processHeader(ByteString& bytes);
	void fillOptionalField(string& header, string& field, string fieldName);
	void trimWhite(string& s);
	string lowerCasify(string s);
	string getMIME(unsigned short flags);
	string getEncod(unsigned short flags);
	bool chunkPost;
	string command;
	string host;
	string accepts;
	string encoding;
	string connection;
	string upgrade;
	string auth;
	string length;
	string key;
	string WSVersion;
};

const unsigned short HTTP_STATUS_MASK = 0xf000, HTTP_OK = 0x1000, HTTP_NOT_FOUND = 0x2000, HTTP_UNAUTH = 0x3000,
		HTTP_MOVED = 0x4000;

const unsigned short HTTP_MIME_MASK = 0xff0, HTTP_MIME_HTML = 0x10, HTTP_MIME_PNG = 0x20, HTTP_MIME_JPEG = 0x30,
		HTTP_MIME_GIF = 0x40, HTTP_MIME_CSS = 0x50, HTTP_MIME_DOWNLOAD = 0x60, HTTP_MIME_JAVASCRIPT = 0x70,
		HTTP_MIME_PLAIN = 0x80;

const unsigned short HTTP_ENCOD_MASK = 0xf, HTTP_ENCOD_BINARY = 0x1;

const unsigned int HTTP_START_BUFFER = 0xfff;
const unsigned int HTTP_MAX_BUFFER = 0xfffff;

#endif /* HTTPCONNECTION_H_ */
